home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / scan-types.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1993-11-03  |  5KB  |  150 lines

  1. #! /bin/sh
  2. # Deduce values of standard ANSI and POSIX types (e.g. size_t, pid_t).
  3. # Emits macros definitions for these, and some other types.
  4. # Intended to be used to massage the sys-protos.h file.
  5. # Expects one arg, which is the GCC source directory.
  6.  
  7. CC=${CC-"./xgcc -B$1/"}
  8. CPP=${CPP-`echo ${CC} -E -I"$1/"`}
  9. SED=sed
  10.  
  11. # Generate definitions for the standard types (such as mode_t)
  12. # compatible with those in the standard C header files.
  13. # It works by a dummy program through the C pre-processor, and then
  14. # using sed to search for typedefs in the output.
  15.  
  16. cat >dummy.c <<!EOF!
  17. #include <sys/types.h>
  18. #include "gstddef.h"
  19. #include "gstdarg.h"
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include <signal.h>
  23. #ifdef size_t
  24. typedef size_t Xsize_t;
  25. #elif defined(__SIZE_TYPE__)
  26. typedef __SIZE_TYPE__ Xsize_t;
  27. #endif
  28. #ifdef ptrdiff_t
  29. typedef ptrdiff_t Xptrdiff_t;
  30. #elif defined(__PTRDIFF_TYPE__)
  31. typedef __PTRDIFF_TYPE__ Xptrdiff_t;
  32. #endif
  33. #ifdef wchar_t
  34. typedef wchar_t Xwchar_t;
  35. #elif defined(__WCHAR_TYPE__)
  36. typedef __WCHAR_TYPE__ Xwchar_t;
  37. #endif
  38. #ifdef va_list
  39. typedef va_list XXXva_list;
  40. #endif
  41. !EOF!
  42.  
  43. if ${CPP} dummy.c >TMP ; then true
  44. else
  45.   echo "gen-params: could not invoke ${CPP} on dummy.c" 1>&2 ; exit 1
  46. fi
  47. tr '    ' ' ' <TMP >dummy.out
  48.  
  49. for TYPE in dev_t clock_t fpos_t gid_t ino_t mode_t nlink_t off_t pid_t ptrdiff_t size_t ssize_t time_t uid_t va_list wchar_t int32_t uint_32_t ; do
  50.     IMPORTED=`eval 'echo $'"$TYPE"`
  51.     if [ -n "${IMPORTED}" ] ; then
  52.     eval "$TYPE='$IMPORTED"
  53.     else
  54.     # Search dummy.out for a typedef for $TYPE, and write it out
  55.     # to TMP in #define syntax.
  56.     rm -f TMP
  57.     ${SED} -n -e "s|.*typedef  *\(.*\) X*$TYPE *;.*|\1|w TMP" <dummy.out>/dev/null
  58.     # Now select the first definition.
  59.         if [ -s TMP ]; then
  60.         # VALUE is now the typedef'd definition of $TYPE.
  61.             eval "VALUE='`${SED} -e 's| *$||' -e '2,$d' <TMP`'"
  62.         # Unless VALUE contains a blank, look for a typedef for it
  63.         # in turn (this could be a loop, but that would be over-kill).
  64.         if echo $VALUE | grep " " >/dev/null ; then true
  65.         else
  66.         rm -f TMP
  67.         ${SED} -n -e "s|.*typedef[     ][     ]*\(.*[^a-zA-Z0-9_]\)${VALUE}[     ]*;.*|\1|w TMP" <dummy.out>/dev/null
  68.         if [ -s TMP ]; then
  69.             eval "VALUE='`${SED} -e '2,$d' -e 's|[     ]*$||' <TMP`'"
  70.         fi
  71.         fi
  72.         eval "$TYPE='$VALUE'"
  73.     fi
  74.     fi
  75. done
  76.  
  77. cat <<!EOF!
  78. #define ${macro_prefix}clock_t ${clock_t-int /* default */}
  79. #define ${macro_prefix}dev_t ${dev_t-int /* default */}
  80. #define ${macro_prefix}fpos_t ${fpos_t-long /* default */}
  81. #define ${macro_prefix}gid_t ${gid_t-int /* default */}
  82. #define ${macro_prefix}ino_t ${ino_t-int /* default */}
  83. #define ${macro_prefix}mode_t ${mode_t-int /* default */}
  84. #define ${macro_prefix}nlink_t ${nlink_t-int /* default */}
  85. #define ${macro_prefix}off_t ${off_t-long /* default */}
  86. #define ${macro_prefix}pid_t ${pid_t-int /* default */}
  87. #define ${macro_prefix}ptrdiff_t ${ptrdiff_t-long int /* default */}
  88. #define ${macro_prefix}size_t ${size_t-unsigned long /* default */}
  89. #define ${macro_prefix}time_t ${time_t-int /* default */}
  90. #define ${macro_prefix}uid_t ${uid_t-int /* default */}
  91. #define ${macro_prefix}wchar_t ${wchar_t-int /* default */}
  92. #define ${macro_prefix}int32_t ${int32_t-int /* default */}
  93. #define ${macro_prefix}uint32_t ${uint32_t-unsigned int /* default */}
  94. !EOF!
  95.  
  96. # (wait_arg_t*) should be (int*), according to Posix, but
  97. # BSD traditionally used (union wait*).  Use (void*) to allow either usage.
  98. echo "#define ${macro_prefix}wait_arg_t void"
  99.  
  100. # ssize_t is the signed version of size_t
  101. if [ -n "${ssize_t}" ] ; then
  102.     echo "#define ${macro_prefix}ssize_t ${ssize_t}"
  103. elif [ -z "${size_t}" ] ; then
  104.     echo "#define ${macro_prefix}ssize_t long"
  105. else
  106.     # Remove "unsigned" from ${size_t} to get ${ssize_t}.
  107.     tmp="`echo ${size_t} | ${SED} -e 's|unsigned||g' -e 's|  | |g'`"
  108.     if [ -z "$tmp" ] ; then
  109.     tmp=int
  110.     else
  111.     # check $tmp doesn't conflict with <unistd.h>
  112.     echo "#include <unistd.h>
  113.     extern $tmp read();" >dummy.c
  114.     ${CC} -c dummy.c >/dev/null 2>&1 || tmp=int
  115.     fi
  116.     echo "#define ${macro_prefix}ssize_t $tmp /* default */"
  117. fi
  118.  
  119. # va_list can cause problems (e.g. some systems have va_list as a struct).
  120. # Check to see if ${va_list-char*} really is compatible with stdarg.h.
  121. cat >dummy.c <<!EOF!
  122. #define X_va_list ${va_list-char* /* default */}
  123. extern long foo(X_va_list ap); /* Check that X_va_list compiles on its own */
  124. #include <stdarg.h>
  125. long foo(X_va_list ap) { return va_arg(ap, long); }
  126. long bar(int i, ...)
  127. { va_list ap; long j; va_start(ap, i); j = foo(ap); va_end(ap); return j; }
  128. !EOF!
  129. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  130.   # Ok: We have something that works.
  131.   echo "#define ${macro_prefix}va_list ${va_list-char* /* default */}"
  132. else
  133.   # No, it breaks.  Indicate that <stdarg.h> must be included.
  134.   echo "#define ${macro_prefix}NEED_STDARG_H
  135. #define ${macro_prefix}va_list va_list"
  136. fi
  137.  
  138. # stuff needed for curses.h
  139.  
  140. # This isn't correct for SVR4 (for example).  However, we only
  141. # use this when adding a missing prototype, so it shouldn't matter.
  142. echo "#define chtype int"
  143. # sys-protos.h uses non-standard names (due to the CHTYPE argument problem).
  144. echo "#define box32 box"
  145. echo "#define initscr32 initscr"
  146. echo "#define w32addch waddch"
  147. echo "#define w32insch winsch"
  148.  
  149. rm -f dummy.c dummy.o TMP dummy.out
  150.